﻿using System;
using Eve.TapToClick.NativeInterop;

namespace Eve.TapToClick.Core
{
    class MouseButtonCase
    {
        // 左键是否处于激活状态
        private bool leftButton { get; set; }
        // 右键是否处于激活状态
        private bool rightButton { get; set; }

        public void UpAll()
        {
   
            if(leftButton)
            {
                SendUpLeft();
            }

            this.leftButton = false;
            this.rightButton = false;
        }

        public void DownLeft()
        {
            this.leftButton = true;
            this.SendDownLeft();
        }

        public void UpLeft()
        {
            this.leftButton = false;
            this.SendUpLeft();
        }
        
        /// <summary>
        /// 鼠标移动
        /// </summary>
        public void SendMouseMove(int x, int y)
        {
            User32.SendInput(new Input
            {
                Type = InputType.Mouse,
                InputValue = new Input.InputUnion
                {
                    MouseInput = new MouseInput
                    {
                        Flags = MouseInputFlag.Move,
                        DeltaX = x,
                        DeltaY = y
                    }
                }
            });
        }

        /// <summary>
        /// 单击鼠标左键
        /// </summary>
        public void SendLeftClick()
        {
            User32.SendInput(new Input
            {
                Type = InputType.Mouse,
                InputValue = new Input.InputUnion
                {
                    MouseInput = new MouseInput
                    {
                        Flags = MouseInputFlag.LeftDown
                    }
                }
            },
            new Input
            {
                Type = InputType.Mouse,
                InputValue = new Input.InputUnion
                {
                    MouseInput = new MouseInput
                    {
                        Flags = MouseInputFlag.LeftUp
                    }
                }
            });
        }

        /// <summary>
        /// 单击鼠标右键
        /// </summary>
        public void SendRightClick()
        {
            User32.SendInput(new Input
            {
                Type = InputType.Mouse,
                InputValue = new Input.InputUnion
                {
                    MouseInput = new MouseInput
                    {
                        Flags = MouseInputFlag.RightDown
                    }
                }
            },
            new Input
            {
                Type = InputType.Mouse,
                InputValue = new Input.InputUnion
                {
                    MouseInput = new MouseInput
                    {
                        Flags = MouseInputFlag.RightUp
                    }
                }
            });
        }

        /// <summary>
        /// Y轴滚动条
        /// </summary>
        public void SendYWheel(int Distance)
        {
            User32.SendInput(new Input
            {
                Type = InputType.Mouse,
                InputValue = new Input.InputUnion
                {
                    MouseInput = new MouseInput
                    {
                        Flags = MouseInputFlag.VerticalWheel,
                        MouseData = Distance
                    }
                }
            });
        }

        /// <summary>
        /// X轴滚动条
        /// </summary>
        public void SendXWheel(int Distance)
        {
            User32.SendInput(new Input
            {
                Type = InputType.Mouse,
                InputValue = new Input.InputUnion
                {
                    MouseInput = new MouseInput
                    {
                        Flags = MouseInputFlag.HorizontalWheel,
                        MouseData = Distance
                    }
                }
            });
        }

        /// <summary>
        /// 按下左键
        /// </summary>
        private void SendDownLeft()
        {
            User32.SendInput(new Input
            {
                Type = InputType.Mouse,
                InputValue = new Input.InputUnion
                {
                    MouseInput = new MouseInput
                    {
                        Flags = MouseInputFlag.LeftDown
                    }
                }
            });
        }

        /// <summary>
        /// 抬起左键
        /// </summary>
        private void SendUpLeft()
        {
            User32.SendInput(new Input
            {
                Type = InputType.Mouse,
                InputValue = new Input.InputUnion
                {
                    MouseInput = new MouseInput
                    {
                        Flags = MouseInputFlag.LeftUp
                    }
                }
            });
        }

    }
}
